package com.comdo.newup_loan.util;

import android.app.Activity;
import android.app.Dialog;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.bank.BankUtils;
import com.comdo.newup_loan.bank.bank.BankApiManager;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.comom.ProgressDialog;
import com.comdo.newup_loan.http.RetrofitHelper;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import retrofit2.Response;


public class DialogUtil {

	public static Dialog getDialg(final Activity context, String msg) {
		final Dialog dialog = new Dialog(context, R.style.MyDialog);
		dialog.show();
		dialog.setCancelable(false);
		Window window = dialog.getWindow();
		window.setContentView(R.layout.dialog_ok);
		TextView dialog_ok = (TextView) window.findViewById(R.id.dialog_ok);
		TextView dialog_no = (TextView) window.findViewById(R.id.dialog_no);
		dialog_no.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();

			}
		});
		TextView tv_nayio = (TextView) window.findViewById(R.id.tv_nayio);
		tv_nayio.setText(msg);
		dialog_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				context.finish();

			}
		});
		return dialog;
	}

	// 提示开通银行存管弹窗
	public static Dialog getToBankRegister(final Activity context) {

		final Dialog dialog = new Dialog(context, R.style.MyDialog);
		dialog.show();
		dialog.setCancelable(true);
		Window window = dialog.getWindow();
		window.setContentView(R.layout.dialog_hasbank);
		Button toRegisterBtn = (Button) window
				.findViewById(R.id.btn_to_register);
		Button toPassBtn = (Button) window.findViewById(R.id.btn_pass);
		TextView tipTextView = (TextView) window.findViewById(R.id.tv_tip);

		if (Config.loginInfo != null
				&& Config.loginInfo.getActivateAccount() == 1) {
			toRegisterBtn.setText("立即激活");
			toPassBtn.setText("暂不激活");
			tipTextView.setText("为了保证您的资金安全，建议您立即激活");
		}

		toRegisterBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				if (Config.loginInfo.getActivateAccount() == 0) {
					CompositeDisposable mCompositeDisposable =new CompositeDisposable();
					ProgressDialog.show(context,false,"正在请求新网银行...");
					FormBody body=BankApiManager.getIstance().personalTieCardRegister(context,Config.loginInfo.getPlatformUserNo());
					mCompositeDisposable.add(RetrofitHelper.createMyBankApi().getWebUrl(body)
							.subscribeOn(Schedulers.io())
							.observeOn(AndroidSchedulers.mainThread())
							.subscribe(new Consumer<Response<String>>() {
								@Override
								public void accept(Response<String> Result) throws Exception  {
									dialog.cancel();
									ProgressDialog.cancle();
									BankUtils.toWebView(Result.headers().get("Location"),context);
								}
							}, new Consumer<Throwable>() {
								@Override
								public void accept(Throwable throwable) throws Exception {;
									ProgressDialog.cancle();
									Toast.makeText(context,"请求失败",Toast.LENGTH_SHORT).show();
								}
							}));
				} else {
					CompositeDisposable mCompositeDisposable =new CompositeDisposable();
					ProgressDialog.show(context,false,"正在请求新网银行...");
					FormBody body=BankApiManager.getIstance().activityUser(context,Config.loginInfo.getPlatformUserNo());
					mCompositeDisposable.add(RetrofitHelper.createMyBankApi().getWebUrl(body)
							.subscribeOn(Schedulers.io())
							.observeOn(AndroidSchedulers.mainThread())
							.subscribe(new Consumer<Response<String>>() {
								@Override
								public void accept(Response<String> Result) throws Exception  {
									BankUtils.toWebView(Result.headers().get("Location"),context);
									dialog.cancel();
									ProgressDialog.cancle();
								}
							}, new Consumer<Throwable>() {
								@Override
								public void accept(Throwable throwable) throws Exception {;
									Toast.makeText(context,"请求失败",Toast.LENGTH_SHORT).show();
									dialog.cancel();
									ProgressDialog.cancle();
								}
							}));
				}

			}
		});

		toPassBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				dialog.dismiss();
			}
		});
		return dialog;
	}

	public static Dialog getOperateConfirmDialg(final Activity context,
												String msg, OnClickListener ok_listener) {
		final Dialog dialog = new Dialog(context, R.style.MyDialog);
		dialog.show();
		dialog.setCancelable(false);
		Window window = dialog.getWindow();
		window.setContentView(R.layout.dialog_operate_confirm);
		TextView dialog_ok = (TextView) window.findViewById(R.id.dialog_ok);
		TextView dialog_no = (TextView) window.findViewById(R.id.dialog_no);
		TextView text_tv = (TextView) window.findViewById(R.id.text_tv);
		text_tv.setText(msg);
		dialog_no.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();

			}
		});
		dialog_ok.setOnClickListener(ok_listener);
		dialog_ok.setTag(dialog);
		return dialog;
	}
	public static int getScreenWidth(Activity context) {
		DisplayMetrics dm = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	public static int getScreenHeight(Activity context) {
		DisplayMetrics dm = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.heightPixels;
	}

}
